package pre.liuxl.blog.utils;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl;
import org.hibernate.cfg.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 单例模式创建SessionFactory
 * Created by liuxuanlong on 2018/6/22.
 */
public class HibernateSessionFactory {
    private final static Logger logger = LoggerFactory.getLogger(HibernateSessionFactory.class);
    private static SessionFactory sessionFactory;


    private HibernateSessionFactory() {
    }

    public static SessionFactory config() {

        if (null == sessionFactory) {
            Configuration configuration = new Configuration();
            configuration.configure();
            logger.info("build sessionFactory");
            return configuration.buildSessionFactory();
        }
        return sessionFactory;

    }

    public static Session getSession() {
        sessionFactory = config();
        return sessionFactory.openSession();
    }
}
